package com.eqdd.yiqidian.ui.gongneng.kehu;

import android.content.Intent;
import android.databinding.DataBindingUtil;

import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.LianxirenInfoActivityCustom;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.LianxirenInfoResultBean;
import com.eqdd.yiqidian.bean.rebuild.LianxirenListResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.HashMap;

public class LianxirenInfoActivity extends SlimBaseMultiEditActivity {
    private LianxirenInfoResultBean items;
    private LianxirenInfoActivityCustom dataBinding;
    private String contactsid;
    private boolean isUpdate = false;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_lianxiren_info);
        initTopTitleBar(View.VISIBLE, "联系人");
        isSave = false;
        isRead = false;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {

    }


    @Override
    public void setView() {
        contactsid = getIntent().getStringExtra("contactsid");
        OkGo.<HttpResult<LianxirenInfoResultBean>>post(HttpConfig.BASE_URL + HttpConfig.LIANXIREN_INFO)
                .params("owner", user.getGuid())
                .params("contactsid", contactsid)
                .execute(new DialogCallBack<HttpResult<LianxirenInfoResultBean>>(LianxirenInfoActivity.this) {


                    @Override
                    public void onSuccess(Response<HttpResult<LianxirenInfoResultBean>> response) {
                        HttpResult<LianxirenInfoResultBean> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            items = httpResult.getItems();
                            datas.add(new SlimEditEnterBean("姓名", items.getName(), 0, ""));
                            datas.add(new SlimEditEnterBean("部门", items.getDep(), 1, ""));
                            datas.add(new SlimEditEnterBean("职务", items.getPost(), 2, ""));
                            datas.add(new SlimEditEnterBean("手机", items.getCellphone(), 3, "").addSchemes(StaticScheme.ChineseMobile()));
                            datas.add(new SlimEditEnterBean("QQ", items.getConqq(), 4, ""));
                            datas.add(new SlimEditEnterBean("微信", items.getConwx(), 5, ""));
                            datas.add(new SlimEditEnterBean("邮箱", items.getEmail(), 6, "").addSchemes(StaticScheme.Email()));
                            datas.add(new SlimEditEnterBean("备注", items.getRemark(), 7, ""));
                            datas.add(new SlimEditEnterBean("创建时间", items.getCreateTime(), 7, ""));
                            slimAdapterEx.updateData(datas);
                        }
                    }
                });
    }

    @Override
    protected void setEditBeans() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    public void update() {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.KEHU_UPDATE_LIANXIREN)
                .params("owner", user.getGuid())
                .params("contactsid", contactsid)
                .params("data",formatParam())
//                .params("name", datas.get(0).getContent())
//                .params("dep", datas.get(1).getContent())
//                .params("post", datas.get(2).getContent())
//                .params("cellphone", datas.get(3).getContent())
//                .params("conqq", datas.get(4).getContent())
//                .params("conwx", datas.get(5).getContent())
//                .params("email", datas.get(6).getContent())
//                .params("remark", datas.get(7).getContent())
                .execute(new DialogCallBack<HttpResult>(LianxirenInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            isUpdate = true;
                        }
                    }
                });
    }

    private String formatParam() {
        HashMap<String, String> map = new HashMap<>();
        map.put("name",datas.get(0).getContent());
        map.put("dep",datas.get(1).getContent());
        map.put("post",datas.get(2).getContent());
        map.put("cellphone",datas.get(3).getContent());
        map.put("conqq",datas.get(4).getContent());
        map.put("conwx",datas.get(5).getContent());
        map.put("email",datas.get(6).getContent());
        map.put("remark",datas.get(7).getContent());
        return new Gson().toJson(map);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.INTENT_CONTENT) {
            String content = data.getStringExtra("content");
            datas.get(requestCode).setContent(content);
            slimAdapterEx.notifyItemChanged(requestCode);
            update();
        }
    }

    @Override
    public void onBackPressed() {
        if (isUpdate) {
            Intent intent = new Intent();
            LianxirenListResultBean lianxirenListResultBean = new LianxirenListResultBean();
            lianxirenListResultBean.setDep(datas.get(1).getContent());
            lianxirenListResultBean.setId(items.getId());
            lianxirenListResultBean.setName(datas.get(0).getContent());
            lianxirenListResultBean.setOwner(items.getOwner());
            lianxirenListResultBean.setPost(datas.get(2).getContent());
            lianxirenListResultBean.setCellphone(datas.get(3).getContent());
            intent.putExtra("bean", lianxirenListResultBean);
            setResult(Config.SUCCESS,intent);
        }
        super.onBackPressed();

    }
}
